const UserService = require('../../services/admin/UserService');
const JWT = require('../../utils/JWT')
const UserController = {
    login: async (req, res) => {
        console.log(req.body)
        const result = await UserService.login(req.body);
        if (result.length === 0) {
            res.send({
                code: "-1",
                msg: "用户名或密码错误"
            })
        } else {
            //生成token
            const token = JWT.generate({
                id: result[0].id,
                username: result[0].username,
            }, '1d');
            //设置token到响应头中
            res.header('Authorization', token);
            res.send({
                code: "0",
                msg: "登录成功",
                data: {
                    id: result[0].id,
                    username: result[0].username,
                    gender: result[0].gender ? result[0].gender : 0,
                    avatar: result[0].avatar,
                    introduction: result[0].introduction,
                    role: result[0].role,
                    createTime: result[0].createTime,
                    updateTime: result[0].updateTime,
                }
            })

        }
    },
    upload: async (req, res) => {
        console.log(req.body, req.file)
        //调用service层上传图片
        const { id, username, gender, introduction } = req.body;
        const avatar = req.file ? `/uploads/avatar/${req.file.filename}` : "";
        try {
            await UserService.upload({ id, username, gender: Number(gender), introduction, avatar });
            console.log(avatar)
            if (avatar) {
                res.send({
                    code: "0",
                    msg: "更新成功!",
                    data: {
                        id, username, gender: Number(gender), introduction, avatar
                    }
                })
            } else {
                res.send({
                    code: "0",
                    msg: "更新成功!",
                    data: {
                        id, username, gender: Number(gender), introduction
                    }
                })
            }
        } catch (error) {
            res.send({
                code: "-1",
                msg: `更新失败! ${error.message}`
            })
        }

    },
    addUser: async (req, res) => {
        const { id, username, password, gender, introduction, role, deleted } = req.body;
        const avatar = req.file ? `/uploads/avatar/${req.file.filename}` : "";
        try {
            await UserService.addUser({ id, username, password, gender: Number(gender), introduction, avatar, role: Number(role), deleted });
            res.send({
                code: "0",
                msg: "添加成功!",
                data: {
                    id, username, gender: Number(gender), introduction, avatar, role: Number(role)
                }
            })
        } catch (errors) {
            res.send({
                code: "-1",
                msg: `添加失败! ${errors.message}`
            })
        }
    },
    findUserList: async (req, res) => {
        const result = await UserService.findUserList();
        res.send({
            code: "0",
            msg: "查询成功!",
            data: result
        })
    },
    deleteUser: async (req, res) => {
        const { id } = req.params;
        try {
            await UserService.deleteUser(id);
            res.send({
                code: "0",
                msg: "删除成功!"
            })
        } catch (error) {
            res.send({
                code: "-1",
                msg: `删除失败! ${error.message}`
            })
        }
    },
    updateUser: async (req, res) => {
        const { _id, username, password, introduction, role } = req.body;
        try {
            await UserService.updateUser({ _id, username, password, introduction, role: Number(role) });
            res.send({
                code: "0",
                msg: "更新成功!",

            })
        } catch (error) {
            res.send({
                code: "-1",
                msg: `更新失败! ${error.message}`
            })
        }

    }
}
module.exports = UserController;